package rent.hj.launcher.statusbar;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.Handler;
import android.text.format.DateFormat;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Set;

import rent.hj.launcher.R;

/**
 * Created by admin on 2017/11/28.
 */

abstract class StatusBarImp implements StatusBar {

    private Handler handler = new Handler();

    private boolean isUsbDeviceInsert = false;

    @Override
    public void onUpdateHeadset(final boolean hasMicrophone, final boolean isActive){
        handler.post(new Runnable() {
            @Override
            public void run() {
                onIUpdateHeadset(hasMicrophone, isActive);
            }
        });
    }

    @Override
    public void onUpdateVolumeZen(){
        handler.post(new Runnable() {
            @Override
            public void run() {
                onIUpdateVolumeZen();
            }
        });
    }

    @Override
    public void onUpdateBluetooth(){

        final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        handler.post(new Runnable() {
            @Override
            public void run() {

                boolean isConnected = false;
                final boolean isEnable = BluetoothAdapter.getDefaultAdapter().isEnabled();

                if (isEnable) {
                    Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
                    for (BluetoothDevice device : bondedDevices) {
                        if (device.isConnected()) {
                            isConnected = true;
                            break;
                        }
                    }
                }
                onIUpdateBluetooth(isEnable, isConnected, -1);
            }
        });

    }

    @Override
    public void onUpdateWiFi(){

    }

    @Override
    public void onUpdateStorage(final String state){
        handler.post(new Runnable() {
            @Override
            public void run() {
                onIUpdateStorage(state);
            }
        });

    }

    @Override
    public void onUpdateBattery(final boolean isCharging, final int percent){
        handler.post(new Runnable() {
            @Override
            public void run() {
                onIUpdateBattery(isCharging, percent);
                if (!isCharging && percent <= 10) {
                    Toast.makeText(mContext, mContext.getString(R.string.battery_low), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private static final String TAG = "StatusBarImp";

    @Override
    public void onUpdateTime(){
        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());

        handler.post(new Runnable() {
            @Override
            public void run() {
                onIUpdateTime((String) DateFormat.format("HH:mm", calendar));
            }
        });
    }

    @Override
    public void onUpdateAirplane(final boolean isAirplane){
        handler.post(new Runnable() {
            @Override
            public void run() {
                onIUpdateAirplane(isAirplane);
            }
        });
    }

    @Override
    public void onUpdateUSB(final boolean isInsert){
        handler.post(new Runnable() {
            @Override
            public void run() {
                onIUpdateUSB(isInsert);

                if (isUsbDeviceInsert == isInsert){
                    return;
                }

                if (isInsert) {
                    Toast.makeText(mContext, mContext.getString(R.string.usb_insert), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(mContext, mContext.getString(R.string.usb_pull_out), Toast.LENGTH_SHORT).show();
                }
                isUsbDeviceInsert = isInsert;
            }
        });
    }

    @Override
    public void onUpdateSDCard(final boolean isInsert){
        handler.post(new Runnable() {
            @Override
            public void run() {
                onIUpdateSDCard(isInsert);
            }
        });

    }

    private Context mContext;

    public void setContext(Context mContext) {
        this.mContext = mContext;
    }

    abstract void onIUpdateHeadset(boolean hasMicrophone, boolean isActive);

    abstract void onIUpdateVolumeZen();

    abstract void onIUpdateBluetooth(boolean isEnable, boolean isConnected, int batteryValue);

    abstract void onIUpdateWiFi(boolean isEnable, boolean isConnected, int RSSI);

    abstract void onIUpdateStorage(String state);

    abstract void onIUpdateBattery(boolean isCharging, int percent);

    abstract void onIUpdateTime(String time);

    abstract void onIUpdateAirplane(boolean isAirplane);

    abstract void onIUpdateUSB(boolean isInsert);

    abstract void onIUpdateSDCard(boolean isInsert);
}
